package com.itic.mall.system.orderexpress.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.code.persistence.dao.CodeDetailDao;
import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.applications.sys.common.persistence.dao.OutCommonDao;
import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.system.order.persistence.dao.OrderDao;
import com.itic.mall.system.order.persistence.model.Order;
import com.itic.mall.system.orderexpress.persistence.dao.OrderExpressDao;
import com.itic.mall.system.orderexpress.persistence.model.OrderExpress;
import com.itic.mall.system.orderexpress.persistence.model.OrderExpressExample;
import com.itic.mall.system.orderexpress.persistence.model.OrderExpressVo;
import com.itic.mall.system.orderproducts.persistence.dao.OrderProductsDao;
import com.itic.mall.system.orderproducts.persistence.model.OrderProducts;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsExample;
import com.itic.mall.system.orderproducts.persistence.model.OrderProductsExample.Criteria;
import com.itic.mall.system.orderproducts.service.OrderProductsService;


@Service
@Transactional
public class OrderExpressService
     extends CrudService<OrderExpressDao,OrderExpress,OrderExpressExample> {
    @Autowired
    private OrderExpressDao orderExpressDao;
    @Autowired
    private OrderProductsDao orderProductsDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OutCommonDao outCommonDao;

	public String addAndUpdateOrder( OrderExpress express) {
		Order order = orderDao.selectByPrimaryKey(express.getOrderId());
		if( null != order ){
			//添加物流信息
			//添加6个必填项
			express.setId(IdGen.uuid());
			express.setCreateTime(new Date());
			express.setUpdateTime(new Date());
			express.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
	                .get(Constants.USER_ID) : "unknown");
			express.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
	                .get(Constants.USER_ORG) : "unknown");
			express.setIsEffective(DBConst.TRUE);
		    
			orderExpressDao.insert(express);
			
			//更新所有的商品这待收货状态
			/*
			OrderProductsExample example = new OrderProductsExample();
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andOrderIdEqualTo(express.getOrderId());
			List<OrderProducts> products = orderProductsDao.selectByExample(example);
			if( products.size() > 0 ){
				for( OrderProducts temp : products ){
					temp.setStatus("2");
					orderProductsDao.updateByPrimaryKeySelective(temp);
				}
			}*/
			OrderProducts orderProducts = orderProductsDao.selectByPrimaryKey(express.getOrderProductsId());
			orderProducts.setStatus("2");
			orderProductsDao.updateByPrimaryKey(orderProducts);
			
			if( isAllStatus( express.getOrderId(), express.getOrderProductsId() ) ){
				order.setStatus("2");//订单为已发货状态
				order.setWuliuId(express.getId());
				orderDao.updateByPrimaryKeySelective(order);
			}
			
		}
		return SUCCESS;
	}

	public String imp(HttpServletRequest request, List<OrderExpressVo> dataList) {
		if( dataList.size()>0 ){
			for( OrderExpressVo tmp : dataList ){
				
				Order order = orderDao.getByOrderNum(tmp.getOrderNum());
				
				if( null != order ){
					OrderExpress express = new OrderExpress();
					CodeDetailVo vo = new CodeDetailVo();
					vo.setName(tmp.getExpressName());
					vo.setType("T_MALL_ORDER_WULIU");
					express.setExpressName(outCommonDao.getCodeByNameType(vo));
					express.setExpressNum(tmp.getExpressNum());
					
					express.setId(IdGen.uuid());
					express.setCreateTime(new Date());
					express.setUpdateTime(new Date());
					express.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
			                .get(Constants.USER_ID) : "unknown");
					express.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
			                .get(Constants.USER_ORG) : "unknown");
					express.setIsEffective(DBConst.TRUE);
				    
					orderExpressDao.insert(express);
					
					order.setStatus("2");//订单为已发货状态
					order.setWuliuId(express.getId());
					orderDao.updateByPrimaryKeySelective(order);
					
					//更新所有的商品这待收货状态
					OrderProductsExample example = new OrderProductsExample();
					example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andOrderIdEqualTo(order.getId());
					List<OrderProducts> products = orderProductsDao.selectByExample(example);
					if( products.size() > 0 ){
						for( OrderProducts temp : products ){
							temp.setStatus("2");
							orderProductsDao.updateByPrimaryKeySelective(temp);
						}
					}
				}
			}
		}
		return SUCCESS;
	}

	/**
	 * 判断是否全为发货状态了,订单下全发货了就，为空
	 * @param orderId
	 * @return
	 */
	public boolean isAllStatus( String orderId,String orderProductsId ){
		boolean result = false;
		if( StringHelper.isNotBlank(orderId) && StringHelper.isNotBlank(orderProductsId) ){
			OrderProductsExample example = new OrderProductsExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andOrderIdEqualTo(orderId);
			criteria.andIdNotEqualTo(orderProductsId);
			criteria.andStatusEqualTo("1");
			List<OrderProducts> selectByExample = orderProductsDao.selectByExample(example);
			if( selectByExample.size()==0 ){
				result = true;
			}
		}
		return result;
	}

}